package com.example.star.mapper;

import com.example.star.entity.BuyNow;
import com.example.star.entity.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BuyNowMapper {
    @Insert("INSERT INTO buy_nows (username,imageResource, starName, title, productType, prices, freight, deliveryAddress,shoppingAddress,quantity,totalPrice,rating,comment,orderStatus) " +
            "VALUES (#{userName},#{imageResource}, #{starName}, #{title}, #{productType}, #{prices}, #{freight}, #{deliveryAddress},#{shoppingAddress},#{quantity},#{totalPrice},#{rating},#{comment},#{orderStatus})")
    void insertBuyNow(BuyNow buyNow);

    @Select("SELECT * FROM buy_nows")
    List<BuyNow> getAllBuyNows();

    @Select("SELECT * FROM buy_nows WHERE id = #{id}")
    BuyNow getBuyNowById(int id);

    @Select("SELECT * FROM buy_nows WHERE username = #{userName}")
    List<BuyNow> getBuyNowByuserName(String userName);

    @Select("SELECT * FROM buy_nows WHERE orderStatus = #{orderStatus}")
    List<BuyNow> getBuyNowByOrderStatus(String orderStatus);

    // 根据订单信息更新订单状态
    @Update("UPDATE buy_nows SET orderStatus = #{orderStatus} WHERE id = #{id}")
    int updateOrderStatus(int id, String orderStatus);

    @Select("SELECT * FROM buy_nows WHERE username = #{userName} AND orderStatus = #{orderStatus}")
    List<BuyNow> getBuyNowsByuserNameAndOrderStatus(String userName, String orderStatus);

    @Select("SELECT * FROM buy_nows LIMIT #{offset}, #{limit}")
    List<BuyNow> getBuyNowsByPage(@Param("offset") int offset, @Param("limit") int limit);
    @Select("SELECT COUNT(*) FROM buy_nows")
    int getTotalBuyNows();
    @Delete("DELETE FROM buy_nows WHERE id = #{id}")
    void deleteBuyNowById(int id);

    @Update("UPDATE buy_nows SET rating = #{rating}, comment = #{comment} WHERE id = #{id}")
    int updateEvaluation(BuyNow buyNow);

    @Delete("DELETE FROM buy_nows WHERE id = #{id}")
    int deleteOrder(int id);

    // 根据用户名分页获取订单信息
    @Select("SELECT * FROM buy_nows WHERE username = #{userName} LIMIT #{offset}, #{size}")
    List<BuyNow> getBuyNowsByUserName(String userName, @Param("offset") int offset, @Param("size") int size);

    // 根据用户名获取订单信息的总数
    @Select("SELECT COUNT(*) FROM buy_nows WHERE username = #{userName}")
    int getTotalBuyNowsByUserName(String userName);
}
